package promotion

import (
	sqlc "bp/db/sqlc"
	sharedConv "bp/internal/rpcserver/shared/conv"
	"bp/internal/service/param"
	promotionGrpc "bp/rpc/gen/mwebapi/mpromotion"
)

type PromotionPtrEntityToPbFunc func(*sqlc.Promotion) *promotionGrpc.PromotionEntity
type PromotionPtrArrayEntityToPbFunc func([]*sqlc.Promotion) []*promotionGrpc.PromotionEntity

// ConvPromotionPtrEntityToPb get func of *sqlc.Promotion => *promotionGrpc.PromotionEntity
func (s *PromotionMWebApiImp) ConvPromotionPtrEntityToPb(
	itemCategoryPtrs param.ItemCategoryConfigurationPtrArray,
	itemPtrs param.ItemConfigurationPtrArray,
) PromotionPtrEntityToPbFunc {
	return func(ptr *sqlc.Promotion) *promotionGrpc.PromotionEntity {
		if ptr == nil {
			return nil
		}
		v := &promotionGrpc.PromotionEntity{
			Id:                      ptr.ID,
			Name:                    ptr.Name,
			Desc:                    ptr.Desc.String,
			Tags:                    ptr.Tags,
			Nature:                  ptr.Nature,
			OfferCategory:           ptr.OfferCategory,
			OfferType:               ptr.OfferType,
			NeedVerifyQualification: ptr.NeedVerifyQualification,
			QualifyTypes:            ptr.QualifyTypes,
			Price:                   ptr.Price,
			Quota:                   ptr.Quota,
			OffMaximum:              ptr.OffMaximum,
			GradientRules:           sharedConv.ConvOffGradientRuleArrayEntityToPb(ptr.GradientRules),
			Begined:                 sqlc.GoTimeToPBTimestamp(ptr.Begined),
			Ended:                   sqlc.GoTimeToPBTimestamp(ptr.Ended),
			TimeRanges:              sharedConv.ConvTimeRangeArrayEntityToPb(ptr.TimeRanges),
			UseScenes:               ptr.UseScenes,
			Scope:                   ptr.Scope,
			ItemCategoryConfs:       sharedConv.ConvItemCategoryConfigurationPtrArrayEntityToPb(itemCategoryPtrs),
			ItemConfs:               sharedConv.ConvItemConfigurationPtrArrayEntityToPb(itemPtrs),
		}
		return v
	}
}

// ConvPromotionPtrEntityToPbWithoutItemInfo get func of *sqlc.Promotion => *promotionGrpc.PromotionEntity
func (s *PromotionMWebApiImp) ConvPromotionPtrEntityToPbWithoutItemInfo() PromotionPtrEntityToPbFunc {
	return func(ptr *sqlc.Promotion) *promotionGrpc.PromotionEntity {
		var itemCategoryPtrs param.ItemCategoryConfigurationPtrArray
		var itemPtrs param.ItemConfigurationPtrArray
		return s.ConvPromotionPtrEntityToPb(itemCategoryPtrs, itemPtrs)(ptr)
	}
}

// ConvPromotionPtrArrayEntityToPb get func of []*sqlc.Promotion => []*promotionGrpc.PromotionEntity
func (s *PromotionMWebApiImp) ConvPromotionPtrArrayEntityToPb(
	itemCategoryMap param.EntityIDToItemCategoryConfigurationPtrsMap,
	itemMap param.EntityIDToItemConfigurationPtrsMap,
) PromotionPtrArrayEntityToPbFunc {
	return func(ptrs []*sqlc.Promotion) (vPtrs []*promotionGrpc.PromotionEntity) {
		var itemCategoryPtrs param.ItemCategoryConfigurationPtrArray
		var itemPtrs param.ItemConfigurationPtrArray
		for _, ptr := range ptrs {
			if itemCategoryMap != nil {
				itemCategoryPtrs = itemCategoryMap[ptr.ID]
			}
			if itemMap != nil {
				itemPtrs = itemMap[ptr.ID]
			}
			vPtrs = append(vPtrs, s.ConvPromotionPtrEntityToPb(itemCategoryPtrs, itemPtrs)(ptr))
		}
		return
	}
}

// ConvPromotionPtrArrayEntityToPb get func of []*sqlc.Promotion => []*promotionGrpc.PromotionEntity
func (s *PromotionMWebApiImp) ConvPromotionPtrArrayEntityToPbWithoutItemInfo() PromotionPtrArrayEntityToPbFunc {
	return func(ptrs []*sqlc.Promotion) (vPtrs []*promotionGrpc.PromotionEntity) {
		var itemCategoryPtrs param.ItemCategoryConfigurationPtrArray
		var itemPtrs param.ItemConfigurationPtrArray
		for _, ptr := range ptrs {
			vPtrs = append(vPtrs, s.ConvPromotionPtrEntityToPb(itemCategoryPtrs, itemPtrs)(ptr))
		}
		return
	}
}
